package com.hzit.proxy.base;

import com.alibaba.fastjson.JSONObject;
import com.hzit.net.RedisUtils;
import com.hzit.net.WXNetUtil;

/**
 * 获取图像二维码
 */
public class ImageQRCode {


    /**
     * 二维码
     *
     * @return
     */
    public static String getImageUrl() {

        String redisKey = "weixin:qrimage";
        String image = RedisUtils.get(redisKey);
        if (image == null) {

            String token = AccessToken.getToken();
            String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + token;
            String param = " {\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"BJ2004_BASE_ID_001\"}}}";

            //{"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm
            //3sUw==","expire_seconds":60,"url":"http://weixin.qq.com/q/kZgfwMTm72WWPkovabbI"}
            String result = WXNetUtil.post(url, param);

            JSONObject jsonObject = JSONObject.parseObject(result);

            if (jsonObject.containsKey("ticket")) {
                String ticket = jsonObject.getString("ticket");
                String imageUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket;
                RedisUtils.set(redisKey, imageUrl);
                return imageUrl;
            }
        }

        return image;
    }

}
